package com.sunnada.edu.system.pub.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.common.Constants;
import com.sunnada.edu.system.pub.dao.BaseDao;
import com.sunnada.edu.system.pub.dao.BaseDaoImpl;
import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.util.DateUtil;
import com.sunnada.edu.system.pub.util.Utils;

public class PubMethods {
	public static String getUserTel(Map user) {
		String tel = "";// 4:表示获取号码失败，0：为非联通号码 1：系统中没有存手机号码 2：获取号码为联通手机号码 3:无此用户
		if (user != null) {
			tel = user.get("TEL") + "";
		}
		if (user != null) {
			String telphone = tel;
			// 判断是否为联通手机号
			if (telphone != null && !"".equals(telphone)) {
				Pattern pattern = Pattern.compile(Constants.PHONE_REGEX);
				Matcher matcher = pattern.matcher(telphone);
				if (matcher.matches()) {
					String t = telphone.substring(0, 3) + "****" + telphone.substring(7, 11);
					tel = "2" + "," + t;
				} else {
					tel = "0";
				}
			} else {
				tel = "1";
			}
		} else {
			tel = "3";
		}
		return tel;
	}

	// 验证用户账号是否可以发送短信
	public static String isCanSendMsg(String type, String userId) {
		String msg = "";
		Pattern pattern = Pattern.compile(Constants.PHONE_REGEX);
		PubDao dao = AppContext.getInstance().getPubDao();
		String tel = "";
		HashMap<String, String> param = new HashMap<String, String>();
		userId = Utils.devExceptString(userId); // 去掉非法字符
		param.put("userId", userId);
		param.put("type", type); // 短信类别
		List l = dao.selectUserTel(param);
		if (l != null && l.size() > 0) {
			tel = (String) ((Map) l.get(0)).get("TEL");
		}
		String telphone = tel;
		Matcher matcher = pattern.matcher(telphone);
		List list = new ArrayList();
		if (matcher.matches()) { // 判断手机号码
			List state = dao.selectUserMsgcodeState(param);
			String mis = "0";
			// 获取短信发送前先判断短信验证码的状态 ：短信验证码1分钟只能发送一次 。相差的时间
			if (state != null && state.size() > 0) {
				String ll = ((Map) state.get(0)).get("MIS").toString();
				mis = ll;
				msg = "3," + mis;
			}
		} else {
			msg = "2,非联通号码，无法发送短信验证码！";
		}
		return msg;

	}

	/*******************************************************************************************************************/

	/**
	 * @param params
	 *            : 必须含有以下属性 String batchId (批次号) String tel (电话) String
	 *            loginAreaCode (区域编码) String sendType (发送类型暂时有 如果有新的类型，提出套路
	 *            请不要私下决定：0.作业 1.消息 2.成绩 3.实时考勤 。。。。) String content (内容) String
	 *            state="0";//状态(0:未处理 1：已处理 2:发送成功 3：发送失败)
	 * @return
	 */
	public static int insertMessage(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String batchId = params.get("batchId");
		String tel = params.get("tel");
		String loginAreaCode = params.get("loginAreaCode");
		String loginId = params.get("loginId");
		String sendType = params.get("sendType");
		String content = params.get("content");
		String state = "0";// 状态(0:未处理 1：已处理)
		String sql = "insert into t_message(BATCH_ID,TEL,AREA_CODE,SEND_TYPE,CONTENT,REC_TIME,REC_STATE,SEND_TIME,REC_PERSON) "
				+ "values('" + batchId + "','" + tel + "','" + loginAreaCode + "','" + sendType + "','" + content + "',sysdate,'" + state
				+ "',sysdate,'" + loginId + "')";
		int num = dao.insert(sql);
		return num;
	}

	/**
	 * @return 返回批次号，用于发送短信用的
	 */
	public static String getBatchId() {
		return DateUtil.getCurrentTime() + "" + (int) (Math.random() * 100);
	}

	
	/**
	 * 
	 * 获取页面路径
	 * 
	 * @param recId
	 * @return
	 */
	public static String getPathValue(String recId) {
		String pathValue = "";
		if (Utils.isNotNull(recId)) {
			PubDao dao = AppContext.getInstance().getPubDao();
			StringBuilder sql = new StringBuilder();
			
			//普通写法
			/*sql.append(" SELECT T2.MODULE_NAME FROM ");
			sql.append("  (SELECT @r AS _id, ");
			sql.append("  ( SELECT @r := parent_id FROM s_sysmodule ");
			sql.append("  WHERE rec_id= _id ");
			sql.append("  ) AS parent_id, ");
			sql.append("  @l := @l + 1 AS lvl ");
			sql.append("  FROM ");
			sql.append("  (SELECT @r := '"+recId+"', @l := 0) vars, s_sysmodule h ");
			sql.append("  ) T1 ");
			sql.append("  JOIN s_sysmodule T2 ON T1._id = T2.rec_id and T2.PARENT_ID is not null  ORDER BY T1.lvl DESC ");*/
			
			//自定义函数写法
			sql.append(" select * from s_sysmodule where rec_id<>'0' and FIND_IN_SET(rec_id,getParLst('"+recId+"')) order by rec_time asc ");
			
			List<HashMap<String, String>> list = (List<HashMap<String, String>>) dao.selectList(sql.toString());
			for (HashMap<String, String> path : list) {
				pathValue += path.get("MODULE_NAME") + ">";
			}
			pathValue = pathValue.substring(0, pathValue.length() - 1);
		}

		return pathValue;
	}
	
	/**
	 * 
	 * 拼接区域的json
	 * @return
	 */
	public static Map getAreaPanel() {
		Map<String,Map> areaObj=new HashMap<String, Map>();
		PubDao dao = AppContext.getInstance().getPubDao();
		//全国
		String sql="SELECT AREA_CODE,AREA_SNAME FROM s_sysarea WHERE area_code LIKE '1%' AND LENGTH(area_code)=3";
		List<HashMap<String, String>> listQG = (List<HashMap<String, String>>)dao.selectList(sql);
		Map <String, String> mapSF= new HashMap<String, String>();
		for(HashMap<String, String> SF : listQG){
			String areaCodeSF=SF.get("AREA_CODE");
			String areaNameSF=SF.get("AREA_SNAME");
			mapSF.put(areaCodeSF,areaNameSF);
			sql="SELECT AREA_CODE,AREA_SNAME FROM s_sysarea WHERE area_code LIKE '"+areaCodeSF+"%' AND LENGTH(area_code)=5";
			List<HashMap<String, String>> listDS = (List<HashMap<String, String>>)dao.selectList(sql);
			Map <String, String> mapDS= new HashMap<String, String>();
			for(HashMap<String, String> DS : listDS){
				String areaCodeDS=DS.get("AREA_CODE");
				String areaNameDS=DS.get("AREA_SNAME");
				mapDS.put(areaCodeDS,areaNameDS);
				sql="SELECT AREA_CODE,AREA_SNAME FROM s_sysarea WHERE area_code LIKE '"+areaCodeDS+"%' AND LENGTH(area_code)=7";
				List<HashMap<String, String>> listQX = (List<HashMap<String, String>>)dao.selectList(sql);
				Map <String, String> mapQX= new HashMap<String, String>();
				for(HashMap<String, String> QX : listQX){
					String areaCodeQX=QX.get("AREA_CODE");
					String areaNameQX=QX.get("AREA_SNAME");
					mapQX.put(areaCodeQX,areaNameQX);
				}
				//地市
				areaObj.put(areaCodeDS, mapQX);
			}
			//省份
			areaObj.put(areaCodeSF, mapDS);
		}
		//全国
		areaObj.put("1", mapSF);
		return areaObj;
	}
	
}
